<?php

namespace app\home\model;
use think\Exception;
use think\Log;
class MyTaskJoin extends Base {
    protected $table = 'tb_task_join';
    private static function _get_where($params = [], $is_count = false) {
        $where = "task_join.uid=" . (isset($params['uid']) ? intval($params['uid']) : 0);
        $where.= " AND task.is_display=1 ";
        if (check_array($params)) {
            if (!empty($params['category_type'])) {
                switch ($params['category_type']) {
                    case "lock":
                        $where.= " AND task_join.status=1 ";
                        break;

                    case "wait":
                        $where.= " AND task_join.status=2 ";
                        break;

                    case "pass":
                        $where.= " AND task_join.status=3 ";
                        break;

                    case "nopass":
                        $where.= " AND task_join.status=4 ";
                        break;

                    default:
                }
        }
    }
    if (!$is_count) {
        $where.= " ORDER BY task_join.update_time DESC";
    }
    return $where;
}
public static function getListByParams($params = [], $psize = 15) {
    $page = get_now_page();
    $pindex = ($page - 1) * $psize;
    $where = self::_get_where($params);
    $sql = "SELECT task.*,task_join.reason as join_reason, task_join.id as join_id,task_join.task_id as on_task_id, task_join.uid as join_uid, task_join.status as join_status, task_join.create_time as join_create_time,task_join.delflag as join_delflag, task_join.audit_time as join_audit_time FROM tb_task_join task_join INNER JOIN tb_task task ON task_join.task_id=task.id WHERE {$where} LIMIT {$pindex},{$psize}";
    $tasks = self::query($sql);
    if (!empty($tasks)) {
        foreach ($tasks as & $v) {
            $v['percent'] = round(floatval($v['join_num'] / $v['ticket_num'] * 100) , 2);
            $v['end_time'] = date('Y-m-d H:i', $v['end_time']);
            $v['join_create_time'] = date('Y-m-d H:i', $v['join_create_time']);
            $v['join_audit_time'] = date('Y-m-d H:i', $v['join_audit_time']);
        }
    }
    return $tasks;
}
public static function getCountByParams($params = [], $psize = 15) {
    try {
        $where = self::_get_where($params, true);
        $sql = "SELECT COUNT(*) AS num FROM tb_task_join task_join INNER JOIN tb_task task ON task_join.task_id=task.id WHERE {$where}";
        $info = self::query($sql);
        return !empty($info[0]['num']) ? $info[0]['num'] : 0;
    }
    catch(Exception $e) {
        Log::error(__FILE__ . ':' . __LINE__ . ' 错误：' . $e->getMessage());
        return null;
    }
}
public static function getLastTaskJoin($uid) {
    return self::where(['uid' => $uid, 'delflag' => 1])->order("id DESC")->limit(1)->find();
}
}

